*
* Table cycles: treatment effects during recessions and expansions
*
set varabbrev off, permanently
clear all
use epfipc2006-2019plus.dta
*
*
global listtotal gasto1-gasto12 year2-year13 ccaa2-ccaa18 income squaredm othehouse ///
       ttenure2-ttenure6 tsize2-tsize5 pdensity2-pdensity3 residshort nmembers nfemales ///
	   nforeign ndworkers n5_15 n16_24 n25_34 n35_64 n65_79 n80omas nstudies2-nstudies4 ///
	   nactive nincome labinc peninc uneinc capinc agemb femalemb foreignmb unemployedmb inactivemb
*
gen tpos=(year<2008 | year>2013) /* expansions */
gen tneg=(year>2007 & year<2014) /* recession */
/* Excluding 2010 as expansion
gen tpos=(year==2008 | year==2010 | year>2013) /* expansions */
gen tneg=((year>2007 & year<2010) | (year>2010 & year<2014)) /* recession */
*/
gen TPOS=f.tpos
gen TNEG=f.tneg
foreach var1 in T10 T21 T01 T12 {
	preserve
	dis in blue "`var1'"
    keep if post==0 & `var1'!=. 
	* propensity score estimation with survey weights
    quietly logit `var1' $listtotal  [pweight=factor]
	predict double ps if e(sample)==1, pr
	drop if ps==.
    gen w=(`var1'+(((1-`var1')*ps)/(1-ps)))
	* Weights used for ATET estimation accounting for survey weights
	gen estweight=w*factor
	* imposing common support
	quietly sum ps if `var1'==1
    scalar pmintreat=r(min)
	drop if ps<pmintreat & `var1'==0
	quietly sum ps if `var1'==0
    scalar pmaxuntreat=r(max)
	drop if ps>pmaxuntreat & `var1'==1
	* estimating ATETs
	gen TNEG`var1'=TNEG*`var1' /* interaction */
	gen TPOS`var1'=TPOS*`var1' /* interaction */
	quietly sum TNEG [aweight=estweight] 
	gen dmtneg=TNEG-r(mean)
	gen intertneg`var1'=dmtneg*`var1'
	quietly sum TPOS [aweight=estweight]
	gen dmtpos=TPOS-r(mean)
	gen intertpos`var1'=dmtpos*`var1'
	gmm (eq1: dgastot - {beta1}*TNEG`var1' - {gamma1}*TPOS`var1'- {alfa1}) ///
    (eq2: dincome - {beta2}*TNEG`var1' - {gamma2}*TPOS`var1' - {alfa2}) ///
	[pweight=estweight], ///
	instruments(eq1: TNEG`var1' TPOS`var1') ///
	instruments(eq2: TNEG`var1' TPOS`var1') winitial(identity) twostep
	nlcom (/beta1)/(/beta2)
	nlcom (/gamma1)/(/gamma2)
	nlcom (/beta1) - (/gamma1)
	nlcom (/beta2) - (/gamma2)
	nlcom (/beta1)/(/beta2) - (/gamma1)/(/gamma2)
	display "Number of treated"
	count if `var1'==1 & e(sample)==1
	display "Number of untreated"
	count if `var1'==0 & e(sample)==1
	restore
}

*** The per adult equivalent results were not included in the paper

clear all
use epfipc2006-2019plus.dta
*
*
global listtotalaeq gastoaeq1-gastoaeq12 year2-year13 ccaa2-ccaa18 income squaredm othehouse ///
       ttenure2-ttenure6 tsize2-tsize5 pdensity2-pdensity3 residshort nmembers nfemales ///
	   nforeign ndworkers n5_15 n16_24 n25_34 n35_64 n65_79 n80omas nstudies2-nstudies4 ///
	   nactive nincome labinc peninc uneinc capinc agemb femalemb foreignmb unemployedmb inactivemb
*
gen tpos=(year<2010 | year>2016) /* low unemployment */
gen tneg=(year>2009 & year<2017) /* high unemployment */
gen TPOS=f.tpos
gen TNEG=f.tneg
foreach var1 in T10 T21 T01 T12  {
	preserve
	dis in blue "`var1'"
    keep if post==0 & `var1'!=. 
	* propensity score estimation with survey weights
    quietly logit `var1' $listtotal  [pweight=factor]
	predict double ps if e(sample)==1, pr
	drop if ps==.
    gen w=(`var1'+(((1-`var1')*ps)/(1-ps)))
	* Weights used for ATET estimation accounting for survey weights
	gen estweight=w*factor
	* imposing common support
	quietly sum ps if `var1'==1
    scalar pmintreat=r(min)
	drop if ps<pmintreat & `var1'==0
	quietly sum ps if `var1'==0
    scalar pmaxuntreat=r(max)
	drop if ps>pmaxuntreat & `var1'==1
	* estimating ATETs
	gen TNEG`var1'=TNEG*`var1' /* interaction */
	gen TPOS`var1'=TPOS*`var1' /* interaction */
	quietly sum TNEG [aweight=estweight] 
	gen dmtneg=TNEG-r(mean)
	gen intertneg`var1'=dmtneg*`var1'
	quietly sum TPOS [aweight=estweight]
	gen dmtpos=TPOS-r(mean)
	gen intertpos`var1'=dmtpos*`var1'
	
	gmm (eq1: dgastotaeq - {beta1}*`var1' - {gamma1}*dmtpos - ///
	{delta1}*intertpos`var1'- {alfa1}) ///
    (eq2: dincomeaeq - {beta2}*`var1' - {gamma2}*dmtpos - ///
	{delta2}*intertpos`var1'- {alfa2}) [pweight=estweight], ///
	instruments(eq1: `var1' dmtpos intertpos`var1') ///
	instruments(eq2: `var1' dmtpos intertpos`var1') winitial(identity) twostep
	nlcom (/beta1)/(/beta2)
	nlcom (/gamma1)/(/gamma2)
	nlcom (/beta1) - (/gamma1)
	nlcom (/beta2) - (/gamma2)
	nlcom (/beta1)/(/beta2) - (/gamma1)/(/gamma2)
	
	display "Number of treated"
	count if `var1'==1 & e(sample)==1
	display "Number of untreated"
	count if `var1'==0 & e(sample)==1
	restore
}

*
* The End
*/
